package wsepForum.ui.swing.outerPanels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;

import wsepForum.network.client.GUIController;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.common.GUIRequest.RequestType;
import wsepForum.ui.swing.SetSwitcher;
import wsepForum.ui.swing.SetSwitcher.SetType;
import wsepForum.ui.swing.innerPanels.AInnerPanel;
import wsepForum.ui.swing.innerPanels.EditUserDetailsPanel;
import wsepForum.ui.swing.workers.ForumWorker;


public class MemberPanel extends AOuterPanel {

	/**
	 *
	 */
	private static final long serialVersionUID = -5081194070761053461L;

	/**
	 * @param connection
	 * @param switcher
	 */
	public MemberPanel(GUIController connection, SetSwitcher switcher) {
		super(connection, switcher);
		addLogoutButton();
		addEditDetailsButton();
	}

	private void addLogoutButton() {
		JButton logoutButton = this.buttonFactory("Logout", 639, 42, 97, 40);
		logoutButton.addActionListener(new ActionListener(){
			boolean loggedout = false;
			@Override
			public void actionPerformed(ActionEvent e) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(connection){
					@Override
					protected AInnerPanel doInBackground() throws Exception {
						GUIAnswer logoutAns = request(RequestType.LOGOUT, new Object[] {});
						setNumOfConnected(logoutAns.getNumOfConnected());
						if((logoutAns.getAnswerType().equals(AnswerType.ANSWER))&&
								(logoutAns.getArguments().length>=1)&&((Boolean)logoutAns.getArguments()[0])){
							loggedout = true;
						}
						else {
							System.err.println(logoutAns.getArguments()[0]);
						}
						return null;
					}
					@Override
					public void done(){
							if (loggedout) {
								switcher.switchSet(SetType.GUEST);
							}
					}
				};
				worker.execute();
		}});
	}

	private void addEditDetailsButton() {

		JButton editDetailsButton = this.buttonFactory("Manage User", 532, 42, 97, 40);
		editDetailsButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(connection){
					@SuppressWarnings("unchecked")
					@Override
					protected AInnerPanel doInBackground() throws Exception {
						GUIAnswer ans = request(RequestType.GET_MNG_USER_LIST, new Object[] {});
						setNumOfConnected(ans.getNumOfConnected());
						AInnerPanel returnedPanel = null;
						if(ans.getAnswerType().equals(AnswerType.ANSWER)){
							returnedPanel = new EditUserDetailsPanel(MemberPanel.this,(Vector<String>)ans.getArguments()[0]);
						}
						else {
							System.err.println(ans.getArguments()[0]);
						}
						return returnedPanel;
					}
					@Override
					public void done(){
						try {
							swapInnerPanel(get());
						} catch (InterruptedException e) {
							e.printStackTrace();
						} catch (ExecutionException e) {
							e.printStackTrace();
						}
					}
				};
				worker.execute();
			}
		});
	}

}
